package com.smmisia.article.adapter;


import cn.dev33.satoken.stp.StpUtil;
import com.smmisia.article.entity.FileInfo;
import com.smmisia.article.utils.MinioUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * minio存储适配器
 * @author smmisia
 * @date 2023/7/5
 */
@Slf4j
public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;

    /**
     * minioUrl
     */
    @Value("${minio.url}")
    private String url;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        String loginId = (String) StpUtil.getLoginId();
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String fileName = "avatar_" + loginId + "." + extension;
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + fileName);
        } else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, fileName);
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        return minioUtil.getAllBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUtil.downLoad(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteObject(String bucket, String objectName) {
        minioUtil.deleteObject(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public String getUrl(String bucket, String objectName) {
        return "http://[2408:871a:b000:2::1:4614]:9000" + "/" + bucket + "/" + objectName;
    }

    @Override
    public void upload(MultipartFile uploadFile, String bucket, String objectName, String uuid) throws Exception {
        try {
            minioUtil.createBucket(bucket);
        } catch (Exception e) {
            log.info("创建bucket失败" + e.getMessage());
            throw new RuntimeException(e);
        }

        String loginId = (String) StpUtil.getLoginId();
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String fileName = loginId + "_" + uuid + "." + extension;
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + fileName);
        } else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, fileName);
        }
    }

    @Override
    public void uploadQrCode(MultipartFile uploadFile, String bucket, String objectName, String uuid) throws Exception {
        minioUtil.createBucket(bucket);
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String fileName = "QrCode_" + uuid + "." + extension;
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + fileName);
        } else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, fileName);
        }
    }

}
